﻿using System.Text.Json;
using System.Text.Json.Serialization;

namespace ORS.OrderRobot
{

    public class JsonDateTimeConverter : JsonConverter<DateTime>
    {
        private readonly string _formatStr = "yyyy-MM-dd HH:mm:ss";

        public JsonDateTimeConverter() { }
        public JsonDateTimeConverter(string formatStr)
        {
            _formatStr = formatStr;
        }
        public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
        {
            return DateTime.Parse(reader.GetString());
        }

        public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options)
        {
            writer.WriteStringValue(value.ToString(_formatStr));
        }
    }
}

